from threading import get_ident, Thread
import time

storage = {}


def set(k, v):
    ident = get_ident()
    if ident in storage:
        storage[ident][k] = v
    else:
        storage[ident] = {k: v}


def get(k):
    ident = get_ident()
    return storage[ident][k]


def task(arg):
    set('val', arg)
    print(storage)
    time.sleep(2)  # 遇到耗时操作也可以正常打印
    v = get('val')
    print(v)


for i in range(10):
    t = Thread(target=task, args=(i,))
    t.start()

"""
通过开辟线程的id当作字典的key,然后对其进行添加
{24244: {'val': 0}}
{24244: {'val': 0}, 19356: {'val': 1}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}, 4720: {'val': 3}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}, 4720: {'val': 3}, 11664: {'val': 4}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}, 4720: {'val': 3}, 11664: {'val': 4}, 20048: {'val': 5}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}, 4720: {'val': 3}, 11664: {'val': 4}, 20048: {'val': 5}, 20884: {'val': 6}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}, 4720: {'val': 3}, 11664: {'val': 4}, 20048: {'val': 5}, 20884: {'val': 6}, 476: {'val': 7}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}, 4720: {'val': 3}, 11664: {'val': 4}, 20048: {'val': 5}, 20884: {'val': 6}, 476: {'val': 7}, 18924: {'val': 8}}
{24244: {'val': 0}, 19356: {'val': 1}, 20748: {'val': 2}, 4720: {'val': 3}, 11664: {'val': 4}, 20048: {'val': 5}, 20884: {'val': 6}, 476: {'val': 7}, 18924: {'val': 8}, 692: {'val': 9}}
51
4
3
0
6
8
9
7
2
"""